# -*- coding: utf-8 -*-
# @Time:2021/04/27 22:24
# @Author:Cindy
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.webdriver import WebDriver
from selenium.common.exceptions import NoSuchElementException


class BasePage:

    def __init__(self, driver: WebDriver = None):
        # 初始化driver
        self.driver = driver

    def find(self, by, *value):
        if isinstance(by, tuple):
            # 如果第一个参数是元组，则解包
            return self.driver.find_element(*by)
        else:
            return self.driver.find_element(by, *value)

    def swip_find(self, text, num=5):
        # 减少隐式等待时间，提高查找速度
        self.driver.implicitly_wait(1)
        for i in range(0, num):
            try:
                element = self.find(text).click()
                # 将隐式等待时间改回去
                self.driver.implicitly_wait(5)
                return element
            except NoSuchElementException:
                print("未找到，开始滑动查找")
                size = self.driver.get_window_size()
                width = size['width']
                height = size['height']
                start_x = width / 2
                start_y = height * 0.8
                end_x = start_x
                end_y = height * 0.3

                self.driver.swipe(start_x, start_y, end_x, end_y, duration=2000)
                if i == num-1:
                    self.driver.implicitly_wait(5)
                    raise NoSuchElementException(f'查找了{i}次，未找到')









